package com.xian.tszs.meal.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xian.tszs.meal.entity.Gscoupon;
import com.xian.tszs.meal.entity.UrCouponRel;
import com.xian.tszs.meal.mapper.GsCouponMapper;
import com.xian.tszs.meal.mapper.UrCouponRelMapper;
import com.xian.tszs.meal.repository.UrCouponRelRepository;
import com.xian.tszs.meal.service.FrontGsCouponService;
import com.xian.tszs.meal.utils.UUIDUtils;
@Service
public class FrontGsCouponServiceImpl implements FrontGsCouponService {
	
	
	@Autowired
	private GsCouponMapper gsCouponMapper;
	
	@Autowired
	private UrCouponRelMapper urCouponRelMapper;
	
	@Autowired
	private UrCouponRelRepository urCouponRelRep;
	
	@Override
	public List<Gscoupon> businessCoupon(String bId) {
		Gscoupon gs = new Gscoupon();
		gs.setBusinessId(bId);
		return gsCouponMapper.findCouponPage(gs);
	}

	@Override
	public Map<String, Object> getCoupon(UrCouponRel cr) {
		
		Map<String, Object> map = new HashMap<>();
		map.put("success", false);
		UrCouponRel isHas = urCouponRelMapper.findByCouponIdAndUserId(cr.getCouponId(),cr.getUserId());
		Gscoupon coupon = gsCouponMapper.findById(cr.getCouponId());
		if (isHas == null && coupon!= null) {
			cr.setId(UUIDUtils.getUUID());
			cr.setUsed(1);
			cr.setCoupon(coupon.getSatisfyPrice());
			cr.setSubtract(coupon.getReducePrice());
			cr.setEndDate(coupon.getEndTime());
			cr.setName(coupon.getName());
			cr.setCreateTime(new Date());
			urCouponRelRep.save(cr);
			map.put("success", true);
			map.put("msg", "优惠券领取成功！");
		} else if (coupon == null) {
			map.put("msg", "不存在的优惠券！");
		} else {
			map.put("msg", "你已经领过一个这个优惠券了！");
		}
		return map;
	}

	@Override
	public List<UrCouponRel> loadUserCoupon(Integer status, String uId) {
		
		return urCouponRelMapper.loadUserCoupon(status,uId);
	}

}
